﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Design;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Drawing.Text;
using System.Windows.Forms;


namespace Radar
{

    class Radar
    {
        bool catchedTargetFlag;
        public bool changeTargetFlag;
        public int targetCatched = 0;    // so hieu cua muc tieu can bam
        public bool clockwiseScan = true;  //chieu quet cua radar
        static int _size;   //size cua man hinh chinh radar
        public Image baseImageRadarScan;    //tao nen man hinh radar
        public Image baseImageStraightSweep;     //tao nen man hinh quet thang
        public Image baseImageCircleSweep;      //tao nen man hinh quet tron
        public Image baseImageSpiralSweep;      //tao nen man hinh quet xoan oc
        public Image outputImageStraightSweep;  //anh quet thang tai 1 thoi diem voi cac muc tieu tren man
        public Image outputImageRadarScan;      //anh radar tai 1 thoi diem voi cac muc tieu tren man
        public Image outputImageCircleSweep;    //anh quet tron tai 1 thoi diem voi cac muc tieu tren man
        public Image outputImageSpiralSweep;    //anh quet xoan oc tai 1 thoi diem voi cac muc tieu tren man
        public int numberOfSpirals=3;           //so vong xoan oc cua anh quet xoan oc, mac dinh la 3
        public double maxAngle = 60;           //goc ngang cao nhat cua radar. mac dinh la 60 do
        public int _az=0;  //goc quet cua radar hien thoi

        public int Sovongtron;
        //ba diem xac dinh canh song cua radar
        public PointF _pt ;     //trung tam canh song - goc phuong vi cua radar
        public PointF _pt2;     //goc phuong vi trung tam -20 
        public PointF _pt3;     //goc phuong vi trung tam +20
        //bo dem thoi gian de xac dinh toc do quet cua radar
        Timer t;
        //mau cua hinh
        Color _topColor = Color.FromArgb(0, 120, 0);
        Color _bottomColor = Color.FromArgb(0, 40, 0);
        Color _lineColor = Color.FromArgb(0, 255, 0);
        Color _targetColor = Color.FromArgb(255, 0, 0);
        //danh sach cac muc tieu

        Graphics g=null;
        public List<FlyingItem> TargetList = new List<FlyingItem>();

        PointF _huong;

        public Radar(int diameter,int ss_w,int ss_h,int cs_size,int sovong)
        {
            //Khoi tao radar voi size cua man scan = diameter, 
            //ss_w= chieu rong anh quet thang,
            //ss_h = chieu cao anh quet thang, 
            //cs_size= do dai canh cua anh quet tron va xoan oc
            _size = diameter;
            Sovongtron = sovong;
            //Khoi tao cac anh nen voi thong so da cho
            if (Sovongtron==3)
            {
                CreateBaseImage3();
            }
            if (Sovongtron == 5)
            {
                CreateBaseImage();
            }
            if (Sovongtron == 6)
            {
                CreateBaseImage6();
            }
            //CreateBaseImage();
            //CreateBaseImageStraightSweep(ss_w, ss_h);
            //CreateBaseImageCircleSweep(cs_size);
            //CreateBaseImageSpiralSweep(cs_size);
            //Tao anh dau ra cua radar
            Draw();
            // khoi tao bo dem thoi gian de ve anh 
            t = new Timer();
            t.Enabled = true;
            t.Tick += new EventHandler(t_Tick);
            t.Interval = 40;

            
        }
        public int DrawScanInterval
        {
            //Khoang thoi gian giua 2 lan quet, cang tang thi duong quet cang cham
            get
            {
                return t.Interval;
            }
            set
            {
                if (value > 0)
                    t.Interval = value;
            }
        }

        
        public Color CustomGradientColorTop
        {
            get
            {
                return _topColor;
            }
            set
            {
                _topColor = value;
                // update the base image
                CreateBaseImage();
                // update the output image
                Draw();
            }
        }

        public Color CustomGradientColorBottom
        {
            get
            {
                return _bottomColor;
            }
            set
            {
                _bottomColor = value;
                // update the base image
                CreateBaseImage();
                // update the output image
                Draw();
            }
        }

        
        
        public Image Image
        {
            get
            {
                return outputImageRadarScan;
            }
        }

        void CreateBaseImage()
        {
            // tao thuc the drawing
            Image i = new Bitmap(_size, _size);
            if (g!=null)
            {
                g.Dispose();
            }
            else
            {
                g = Graphics.FromImage(i);
            }
            //Graphics g = Graphics.FromImage(i);
            Pen p = new Pen(_lineColor);
            //lam anh dep hon
            g.CompositingQuality = CompositingQuality.HighQuality;
            g.InterpolationMode = InterpolationMode.Bicubic;
            g.SmoothingMode = SmoothingMode.AntiAlias;
            g.TextRenderingHint = TextRenderingHint.AntiAlias;
            // ve anh nen cua anh radar scan
            
            g.DrawEllipse(p, 0, 0, _size - 1, _size - 1);
            // ve cac vong tron ( o day la 5 vong tron)
            int interval = _size / Sovongtron;
            Font ab = new Font("Arial", 10);
            for (int j = 1; j < Sovongtron; j++)
            {
                int interval1 = interval * j;
                g.DrawEllipse(p, (_size - interval1) / 2, (_size - interval1) / 2, interval1, interval1);
                //Font ab = new Font("Arial", 10);
                //g.DrawString((30 - 6 * j).ToString() + "0", ab, Brushes.Red, interval1 / 2, _size / 2);
                //g.DrawString((30 - 6 * j).ToString() + "0", ab, Brushes.Red, _size - interval1 / 2 - 15, _size / 2);
                if (j==1)
                {
                    g.DrawString("180", ab, Brushes.Red, 225, 30);
                }
                if (j==2)
                {
                    g.DrawString("135" , ab, Brushes.Red, 225, 75);
                }
                if (j == 3)
                {
                    g.DrawString("92", ab, Brushes.Red, 225, 120);
                }
                if (j == 4)
                {
                    g.DrawString("47" , ab, Brushes.Red, 225, 165);
                }
            }

            // ve truc Ox va truc Oy
            g.DrawLine(p, new Point(0, (int)(_size / 2)), new Point(_size - 1, (int)(_size / 2)));            
            g.DrawLine(p, new Point((int)(_size / 2), 0), new Point((int)(_size / 2), _size - 1));

            g.DrawLine(p, new Point((int)223, 223), new Point(337, 32));
            g.DrawString("30", ab, Brushes.Red, 337, 20);

            g.DrawLine(p, new Point((int)223, 223), new Point(417, 114));
            g.DrawString("60", ab, Brushes.Red, 418, 105);

            //120 do
            g.DrawLine(p, new Point((int)223, 223), new Point(417, 334));
            g.DrawString("120", ab, Brushes.Red, 416, 333);

            //150 d0
            g.DrawLine(p, new Point((int)223, 223), new Point(334, 416));
            g.DrawString("150", ab, Brushes.Red, 333, 417);

            //210 do
            g.DrawLine(p, new Point((int)223, 223), new Point(111, 416));
            g.DrawString("210", ab, Brushes.Red, 93, 419);

            //240
            g.DrawLine(p, new Point((int)223, 223), new Point(29, 333));
            g.DrawString("240", ab, Brushes.Red, 4, 327);

            //300
            g.DrawLine(p, new Point((int)223, 223), new Point(33, 110));
            g.DrawString("300", ab, Brushes.Red, 8, 96);

            //330
            g.DrawLine(p, new Point((int)223, 223), new Point(114, 29));
            g.DrawString("330", ab, Brushes.Red, 93, 15);

            Font abc = new Font("Arial", 10);

            g.DrawString("W", abc, Brushes.Red, 0, _size / 2);
            g.DrawString("N", abc, Brushes.Red, _size / 2,0);
            g.DrawString("E", abc, Brushes.Red, _size - 15, _size / 2);
            g.DrawString("S", abc, Brushes.Red, _size / 2, _size - 15);
            // xoa anh ve
            g.Dispose();
            // update the base image
            baseImageRadarScan = i;
        }
        void CreateBaseImage3()
        {
            // tao thuc the drawing
            Image i = new Bitmap(_size, _size);
            if (g != null)
            {
                g.Dispose();
            }
            else
            {
                g = Graphics.FromImage(i);
            }
            //Graphics g = Graphics.FromImage(i);
            Pen p = new Pen(_lineColor);
            //lam anh dep hon
            g.CompositingQuality = CompositingQuality.HighQuality;
            g.InterpolationMode = InterpolationMode.Bicubic;
            g.SmoothingMode = SmoothingMode.AntiAlias;
            g.TextRenderingHint = TextRenderingHint.AntiAlias;
            // ve anh nen cua anh radar scan

            g.DrawEllipse(p, 0, 0, _size - 1, _size - 1);
            // ve cac vong tron ( o day la 5 vong tron)
            int interval = _size / 3;
            Font ab = new Font("Arial", 10);
            for (int j = 1; j < 3; j++)
            {
                int interval1 = interval * j;
                g.DrawEllipse(p, (_size - interval1) / 2, (_size - interval1) / 2, interval1, interval1);
                //Font ab = new Font("Arial", 10);
                //g.DrawString((30 - 6 * j).ToString() + "0", ab, Brushes.Red, interval1 / 2, _size / 2);
                //g.DrawString((30 - 6 * j).ToString() + "0", ab, Brushes.Red, _size - interval1 / 2 - 15, _size / 2);
                if (j == 1)
                {
                    g.DrawString("180", ab, Brushes.Red, _size / 2, (_size/3)-15);
                }
                if (j == 2)
                {
                    g.DrawString("135", ab, Brushes.Red, _size / 2, (_size / 6)-15);
                }
            }

            // ve truc Ox va truc Oy
            g.DrawLine(p, new Point(0, (int)(_size / 2)), new Point(_size - 1, (int)(_size / 2)));
            g.DrawLine(p, new Point((int)(_size / 2), 0), new Point((int)(_size / 2), _size - 1));

            _huong = huong(30, _size);
            g.DrawLine(p, new Point((int)_size / 2, _size / 2), new Point(Convert.ToInt16(_huong.X),Convert.ToInt16(_huong.Y)));
            g.DrawString("30", ab, Brushes.Red, new Point(Convert.ToInt16(_huong.X), Convert.ToInt16(_huong.Y)-12)); //337, 20);

            _huong = huong(60, _size);
            g.DrawLine(p, new Point((int)_size / 2, _size / 2), new Point(Convert.ToInt16(_huong.X), Convert.ToInt16(_huong.Y)));
            g.DrawString("60", ab, Brushes.Red, Convert.ToInt16(_huong.X), Convert.ToInt16(_huong.Y)-8);//418, 105);

            _huong = huong(120, _size);
            //120 do
            g.DrawLine(p, new Point((int)_size / 2, _size / 2), new Point(Convert.ToInt16(_huong.X), Convert.ToInt16(_huong.Y)));
            g.DrawString("120", ab, Brushes.Red, Convert.ToInt16(_huong.X), Convert.ToInt16(_huong.Y));//416, 333);

            _huong = huong(150, _size);
            //150 d0
            g.DrawLine(p, new Point((int)_size / 2, _size / 2), new Point(Convert.ToInt16(_huong.X), Convert.ToInt16(_huong.Y)));
            g.DrawString("150", ab, Brushes.Red, Convert.ToInt16(_huong.X), Convert.ToInt16(_huong.Y));//333, 417);

             _huong = huong(210, _size);
            //210 do
            g.DrawLine(p, new Point((int)_size / 2, _size / 2), new Point(Convert.ToInt16(_huong.X), Convert.ToInt16(_huong.Y)));
            g.DrawString("210", ab, Brushes.Red, Convert.ToInt16(_huong.X), Convert.ToInt16(_huong.Y)+2);//93, 419);

            _huong = huong(240, _size);
            //240
            g.DrawLine(p, new Point((int)_size / 2, _size / 2), new Point(Convert.ToInt16(_huong.X), Convert.ToInt16(_huong.Y)));
            g.DrawString("240", ab, Brushes.Red, Convert.ToInt16(_huong.X), Convert.ToInt16(_huong.Y));//4, 327);

            _huong = huong(300, _size);
            //300
            g.DrawLine(p, new Point((int)_size / 2, _size / 2), new Point(Convert.ToInt16(_huong.X), Convert.ToInt16(_huong.Y)));
            g.DrawString("300", ab, Brushes.Red, Convert.ToInt16(_huong.X), Convert.ToInt16(_huong.Y));//8, 96);

            _huong = huong(330, _size);
            //330
            g.DrawLine(p, new Point((int)_size / 2, _size / 2), new Point(Convert.ToInt16(_huong.X), Convert.ToInt16(_huong.Y)));
            g.DrawString("330", ab, Brushes.Red, Convert.ToInt16(_huong.X), Convert.ToInt16(_huong.Y));//93, 15);

            Font abc = new Font("Arial", 10);

            g.DrawString("W", abc, Brushes.Red, 0, _size / 2);
            g.DrawString("N", abc, Brushes.Red, _size / 2, 0);
            g.DrawString("E", abc, Brushes.Red, _size - 15, _size / 2);
            g.DrawString("S", abc, Brushes.Red, _size / 2, _size - 15);
            // xoa anh ve
            g.Dispose();
            // update the base image
            baseImageRadarScan = i;
        }
        void CreateBaseImage6()
        {
            // tao thuc the drawing
            Image i = new Bitmap(_size, _size);
            if (g != null)
            {
                g.Dispose();
            }
            else
            {
                g = Graphics.FromImage(i);
            }
            //Graphics g = Graphics.FromImage(i);
            Pen p = new Pen(_lineColor);
            //lam anh dep hon
            g.CompositingQuality = CompositingQuality.HighQuality;
            g.InterpolationMode = InterpolationMode.Bicubic;
            g.SmoothingMode = SmoothingMode.AntiAlias;
            g.TextRenderingHint = TextRenderingHint.AntiAlias;
            // ve anh nen cua anh radar scan

            g.DrawEllipse(p, 0, 0, _size - 1, _size - 1);
            // ve cac vong tron ( o day la 5 vong tron)
            int interval = _size / 6;
            Font ab = new Font("Arial", 10);
            for (int j = 1; j < 6; j++)
            {
                int interval1 = interval * j;
                g.DrawEllipse(p, (_size - interval1) / 2, (_size - interval1) / 2, interval1, interval1);
                //Font ab = new Font("Arial", 10);
                //g.DrawString((30 - 6 * j).ToString() + "0", ab, Brushes.Red, interval1 / 2, _size / 2);
                //g.DrawString((30 - 6 * j).ToString() + "0", ab, Brushes.Red, _size - interval1 / 2 - 15, _size / 2);
                if (j == 1)
                {
                    g.DrawString("180", ab, Brushes.Red, 225, 30);
                }
                if (j == 2)
                {
                    g.DrawString("135", ab, Brushes.Red, 225, 75);
                }
                if (j == 3)
                {
                    g.DrawString("92", ab, Brushes.Red, 225, 120);
                }
                if (j == 4)
                {
                    g.DrawString("47", ab, Brushes.Red, 225, 165);
                }
            }

            // ve truc Ox va truc Oy
            g.DrawLine(p, new Point(0, (int)(_size / 2)), new Point(_size - 1, (int)(_size / 2)));
            g.DrawLine(p, new Point((int)(_size / 2), 0), new Point((int)(_size / 2), _size - 1));

            g.DrawLine(p, new Point((int)223, 223), new Point(337, 32));
            g.DrawString("30", ab, Brushes.Red, 337, 20);

            g.DrawLine(p, new Point((int)223, 223), new Point(417, 114));
            g.DrawString("60", ab, Brushes.Red, 418, 105);

            //120 do
            g.DrawLine(p, new Point((int)223, 223), new Point(417, 334));
            g.DrawString("120", ab, Brushes.Red, 416, 333);

            //150 d0
            g.DrawLine(p, new Point((int)223, 223), new Point(334, 416));
            g.DrawString("150", ab, Brushes.Red, 333, 417);

            //210 do
            g.DrawLine(p, new Point((int)223, 223), new Point(111, 416));
            g.DrawString("210", ab, Brushes.Red, 93, 419);

            //240
            g.DrawLine(p, new Point((int)223, 223), new Point(29, 333));
            g.DrawString("240", ab, Brushes.Red, 4, 327);

            //300
            g.DrawLine(p, new Point((int)223, 223), new Point(33, 110));
            g.DrawString("300", ab, Brushes.Red, 8, 96);

            //330
            g.DrawLine(p, new Point((int)223, 223), new Point(114, 29));
            g.DrawString("330", ab, Brushes.Red, 93, 15);

            Font abc = new Font("Arial", 10);

            g.DrawString("W", abc, Brushes.Red, 0, _size / 2);
            g.DrawString("N", abc, Brushes.Red, _size / 2, 0);
            g.DrawString("E", abc, Brushes.Red, _size - 15, _size / 2);
            g.DrawString("S", abc, Brushes.Red, _size / 2, _size - 15);
            // xoa anh ve
            g.Dispose();
            // update the base image
            baseImageRadarScan = i;
        }
        void Draw()
        {
            Image scanImage;
            if (!catchedTargetFlag)
                scanImage = (Image)baseImageRadarScan.Clone();
            else
            {
                if (changeTargetFlag)
                    scanImage = (Image)baseImageRadarScan.Clone();
                else
                    scanImage = outputImageRadarScan;
            }
            //Image straightSweepImage = (Image)baseImageStraightSweep.Clone();
            //Image circleSweepImage = (Image)baseImageCircleSweep.Clone();
            //Image spiralSweepImage = (Image)baseImageSpiralSweep.Clone();
            
            Graphics scanGraphic = Graphics.FromImage(scanImage);
            //Graphics straightSweepGraphic = Graphics.FromImage(straightSweepImage);
            //Graphics circleSweepGraphic = Graphics.FromImage(circleSweepImage);
            //Graphics spiralSweepGraphic = Graphics.FromImage(spiralSweepImage);
            
            GraphicsPath gp = new GraphicsPath(FillMode.Winding);

            if (!catchedTargetFlag && _pt2.X != _size / 2 && _pt2.X != _size && _pt2.Y != _size / 2)
            {
                //ve canh song radar
                gp.AddLine(new PointF((float)(_size / 2), (float)(_size / 2)), _pt2);
                //gp.AddCurve(new PointF[] { _pt2, AzRg2XY(_az - 20, _size / 2), AzRg2XY(_az - 10, _size / 2), _pt, AzRg2XY(_az + 10, _size / 2), AzRg2XY(_az + 20, _size / 2), _pt3 });
                //gp.AddLine(_pt3, new PointF((float)(_size / 2), (float)(_size / 2)));
                PathGradientBrush pgb = new PathGradientBrush(gp);
                pgb.CenterPoint = _pt3;
                pgb.CenterColor = Color.FromArgb(128, _lineColor);
                pgb.SurroundColors = new Color[] { Color.Empty };
                scanGraphic.FillPath(pgb, gp);
                // ve tia quet
                scanGraphic.DrawLine(new Pen(_lineColor), new PointF((float)(_size / 2), (float)(_size / 2)), _pt3);

                //check
                double r, x, y;
                r = _size * 0.5d;//ban kinh duong tron
                //xac dinh toa do so voi tam
                x = _pt3.X - r;
                y = _pt3.Y - r;
                double canh_huyen = Math.Sqrt(x * x + y * y);
                //
            }
            else 
            {
                
                if (changeTargetFlag)
                {
                    //ve canh song radar
                    gp.AddLine(new PointF((float)(_size / 2), (float)(_size / 2)), _pt2);
                    gp.AddCurve(new PointF[] { _pt2, AzRg2XY(_az - 20, _size / 2), AzRg2XY(_az - 10, _size / 2), _pt, AzRg2XY(_az + 10, _size / 2), AzRg2XY(_az + 20, _size / 2), _pt3 });
                    gp.AddLine(_pt3, new PointF((float)(_size / 2), (float)(_size / 2)));
                    PathGradientBrush pgb = new PathGradientBrush(gp);
                    pgb.CenterPoint = _pt3;
                    pgb.CenterColor = Color.FromArgb(128, _lineColor);
                    pgb.SurroundColors = new Color[] { Color.Empty };
                    scanGraphic.FillPath(pgb, gp);
                    // ve tia quet
                    scanGraphic.DrawLine(new Pen(_lineColor), new PointF((float)(_size / 2), (float)(_size / 2)), _pt3);
                }
            }
            //tinh sin cua goc ngang cao nhat
            double max_angle = maxAngle * 0.0174532925d;      //doi sang radian
            
            foreach (FlyingItem item in TargetList)
            {
                item.DrawItem(this, scanGraphic,pathIcon);
                //item.DrawItemStraightSweep(this, straightSweepGraphic, straightSweepImage.Width, straightSweepImage.Height);
                //item.DrawItemCircleSweep(this, circleSweepGraphic, circleSweepImage.Width - 40);
                //item.DrawItemSpiralSweep(this, spiralSweepGraphic, spiralSweepImage.Width - 40, numberOfSpirals);

                currentObject = item._current;

                //neu muc tieu nam trong canh song cua radar thi hien thi tren man hinh va hien thi tren man scan
                //if (_az + 20 > 360)
                //{
                //    if (getAzimuth(item._current) < 360 && getAzimuth(item._current) > getAzimuth(_pt2) && (getRange(item._current) * Math.Sin(max_angle)) > item._current.Z && getRange(item._current) < _size / 2)
                //    {
                //        item.DrawItem(this, scanGraphic, pathIcon);
                //    }
                //}
                //else
                //{
                //    if (getAzimuth(item._current) < getAzimuth(_pt3) && getAzimuth(item._current) > getAzimuth(_pt2) && (getRange(item._current) * Math.Sin(max_angle)) > item._current.Z && getRange(item._current) < _size / 2)
                //    {
                //        item.DrawItem(this, scanGraphic, pathIcon);
                //    }

                //}
                if (getAzimuth(item._current) < 360 && getAzimuth(item._current) > getAzimuth(_pt2) && (getRange(item._current) * Math.Sin(max_angle)) > item._current.Z && getRange(item._current) < _size / 2)
                {
                    item.DrawItem(this, scanGraphic, pathIcon);
                }
            }
            
            // ve lai vong tron ( chi de anh dep hon)
           

            // giai phong tai nguyen
            scanGraphic.Dispose();
            //straightSweepGraphic.Dispose();
            //circleSweepGraphic.Dispose();
            //spiralSweepGraphic.Dispose();
            // goi ham update anh
            OnImageUpdate(scanImage);
            //outputImageCircleSweep = circleSweepImage;
            //outputImageStraightSweep = straightSweepImage;
            //outputImageSpiralSweep = spiralSweepImage;
            //outputImageRadarScan = scanImage;
            if (targetCatched == 0)
                catchedTargetFlag = false;
            else
            {
                catchedTargetFlag = true;
                changeTargetFlag = false;
            }
        }
        public PointF huong(int huong,int widthPictrueBox)
        {
            int _size = widthPictrueBox;
            //chuyen goc phuong vi sang goc theo truc X
            double angle = (270d + (double)huong);

            // chuyen sang radian
            angle *= 0.0174532925d;

            double r, x, y;

            // tinh x va y theo size cua man hinh scan radar co san
            r = _size * 0.5;

            _size = widthPictrueBox;

            x = (((double)_size * 0.5d) + (r * Math.Cos(angle)));
            y = (((double)_size * 0.5d) + (r * Math.Sin(angle)));

            return new PointF((float)x, (float)y);
        }
        // ve cac vach chia cua man hinh quet thang cung voi so km
        public void DrawStraightSweepDegree(Graphics g, int x, int y, string str)
        {
            g.DrawLine(new Pen(Color.Red), x , y-7, x, y+7);
            g.DrawString(str, new Font("Arial", 8), Brushes.Red, (float)x-10, (float)y+10);
        }

        // ve anh nen cua man hinh quet thang
        public void CreateBaseImageStraightSweep(int w,int h)
        {
            Image i = new Bitmap(w,h);
            Graphics g = Graphics.FromImage(i);
            Pen p = new Pen(_lineColor);
            
            //ve ba duong thang
            g.DrawLine(p,new Point(0,h/4),new Point(w,h/4));
            g.DrawLine(p, new Point(0, h / 2), new Point(w, h / 2));
            g.DrawLine(p, new Point(0, 3*h / 4), new Point(w, 3*h / 4));
            //ve cac diem chia tren cac duong thang
            //duong thang 1

            DrawStraightSweepDegree(g, w / 3, h / 4, "100");
            DrawStraightSweepDegree(g, 2*w / 3, h / 4, "200");
            
            //duong thang 2
            for (int k = 1; k < 10; k++)
            {
                DrawStraightSweepDegree(g,k* w / 10, h / 2, k.ToString()+"0");
            }
            //duong thang 3
            for (int k = 1; k < 10; k++)
            {
                DrawStraightSweepDegree(g, k * w / 10, 3*h / 4, k.ToString() );
            }
            g.Dispose();
            baseImageStraightSweep=i;
        }
        //ve anh nen cua man hinh quet tron
        public void CreateBaseImageCircleSweep(int w)
        {
            Image i = new Bitmap(w, w);
            int size = w - 40;
            Graphics g = Graphics.FromImage(i);
            Pen p = new Pen(_lineColor);
            
            //ve 3 vong tron
            g.DrawEllipse(new Pen(Color.Green), 20, 20, size,size);
            g.DrawEllipse(new Pen(Color.Green), size / 6+20, size /6+20, size * 2 / 3, size * 2 / 3);
            g.DrawEllipse(new Pen(Color.Green), size / 3 + 20, size / 3 + 20, size / 3, size / 3);
            //ve cac vach chia
            //vong tron ngoai cung
            DrawCircleSweepDegree(g, 120, size/2, "100",w);
            DrawCircleSweepDegree(g, 240, size / 2, "200", w);
            DrawCircleSweepDegree(g, 0, size / 2, "300", w);
            g.DrawString("0", new Font("Arial", 8), Brushes.Red, AzRg2XY(0, size/2+20, w));
            //vong tron thu 2
            for (int k = 1; k <= 10; k++)
            {
                DrawCircleSweepDegree(g, 36*k, size / 3, k.ToString()+"0", w);
            }
            g.DrawString("0", new Font("Arial", 8), Brushes.Red, AzRg2XY(0, size / 3 + 20, w));
            //vong tron thu 3
            for (int k = 1; k <= 10; k++)
            {
                DrawCircleSweepDegree(g, 36 * k, size / 6, k.ToString(), w);
            }
            g.DrawString("0", new Font("Arial", 8), Brushes.Red, AzRg2XY(0, size / 6 + 20, w));
            g.Dispose();
            baseImageCircleSweep= i;
        }
        // ve cac vach chia cua man hinh quet tron
        private void DrawCircleSweepDegree(Graphics g, int az, int rg, string str,int size)
        {
            PointF pt1 = AzRg2XY(az, rg - 10,size);
            PointF pt2 = AzRg2XY(az, rg + 10,size);
            g.DrawLine(new Pen(Color.Red), pt1, pt2);
            g.DrawString(str, new Font("Arial", 8), Brushes.Red, AzRg2XY(az,rg,size));
        }
        //ve anh nen cua man hinh quet xoan oc
        public void CreateBaseImageSpiralSweep(int w)
        {

            Image i = new Bitmap(w, w);
            int size = w - 40;
            Graphics g = Graphics.FromImage(i);
            Pen p = new Pen(_lineColor);
            
            int iNumRevs   = numberOfSpirals;
            int       iNumPoints = 300;
            PointF[]  aptf       = new PointF[iNumPoints];
            float     fAngle, fScale;
   
            for (int k = 0; k < iNumPoints; k++)
            {
                fAngle = (float)(k * 2 * Math.PI /(iNumPoints / iNumRevs));
                fScale = 1 - (float)k / iNumPoints;   
                aptf[k].X = (float)((size/2) * (1 + fScale * Math.Sin(fAngle)));
                aptf[k].Y = (float)((size/2) * (1 + fScale * Math.Cos(fAngle)));
                if (k % 10 == 0)
                {
                    g.DrawString((300 - k).ToString(), new Font("Arial", 7), Brushes.Red, aptf[k]);
                    
                }
                if (k % 2 == 0)
                {
                    PointF pt1 = new PointF((float)((size / 2) * (1 + fScale * 1.05 * Math.Sin(fAngle))), (float)((size / 2) * (1 + fScale * 1.05 * Math.Cos(fAngle))));
                    PointF pt2 = new PointF((float)((size / 2) * (1 + fScale * 0.95 * Math.Sin(fAngle))), (float)((size / 2) * (1 + fScale * 0.95 * Math.Cos(fAngle))));
                    g.DrawLine(new Pen(Color.Green), pt1, pt2);
                }
            }
            g.DrawLines(new Pen(Color.Green), aptf);
            
            g.Dispose();
            baseImageSpiralSweep= i;
     
        }
        //them cac muc tieu vao list muc tieu cua radar
        public void AddItem(FlyingItem item)
        {
            bool bFlag = true;

            for (int i = 0; i < TargetList.Count; i++)
            {
                if (TargetList[i].ID == item.ID)
                {
                    TargetList[i] = item;
                    bFlag = false;
                }
                
            }

            if (bFlag)
                TargetList.Add(item);
            
            Draw();
        }
        // event xay ra khi bo dem nhay 1 khoang thoi gian
        void t_Tick(object sender, EventArgs e)
        {
            //neu khong co muc tieu bam
            if (targetCatched == 0)
            {
               
                // tang hoac giam goc quet
                if (clockwiseScan)
                {
                    _az++;
                }
                else
                {
                    _az--;
                }
                // reset goc phuong vi
                // neu khong xuat hien loi OverflowException
                if (_az >= 360)
                    _az = 0;

                // tao 3 diem de xac dinh canh song radar
                _pt = AzRg2XY(_az, _size/2);
                _pt2 = AzRg2XY(_az-20, _size/2);
                _pt3 = AzRg2XY(_az+20, _size/2);

                MainForm.culy = _pt3.X;
                MainForm.phuongvi = _pt3.Y;

                // ve lai anh dau ra
                Draw();
            }
                
            // neu co muc tieu bam
            else
            {
                
                foreach (FlyingItem item in TargetList)
                {
                    if (item.ID == targetCatched)
                        _az = getAzimuth(item._current);
                }
                 
                _pt = AzRg2XY(_az, _size/2);
                _pt2 = AzRg2XY(_az-20, _size/2);
                _pt3 = AzRg2XY(_az+20, _size/2);
                
                Draw();
            }
        }

        // ham chuyen doi tu  goc phuong vi - cu ly sang  X-Y neu size = size cua man hinh scan radar
        public PointF AzRg2XY(int azimuth, int Rg)
        {
            //chuyen goc phuong vi sang goc theo truc X
            double angle = (270d + (double)azimuth);

            // chuyen sang radian
            angle *= 0.0174532925d;

            double r, x, y;

            // tinh x va y theo size cua man hinh scan radar co san
            r = Rg;

            x = (((double)_size * 0.5d) + (r * Math.Cos(angle)));
            y = (((double)_size * 0.5d) + (r * Math.Sin(angle)));

            return new PointF((float)x, (float)y);
        }
        // ham chuyen doi tu  goc phuong vi - cu ly sang  X-Y neu size = size cho truoc ( cac class khac co the su dung de tinh toan)
        public PointF AzRg2XY(int azimuth, int Rg,int size)
        {
            //chuyen sang goc so voi truc X
            double angle = (270d + (double)azimuth);

            // chuyen sang radian
            angle *= 0.0174532925d;

            double r, x, y;

            // Tinh X va Y
            r = Rg;

            x = (((double)size * 0.5d) + (r * Math.Cos(angle)));
            y = (((double)size * 0.5d) + (r * Math.Sin(angle)));

            return new PointF((float)x, (float)y);
        }
        public static bool isNummeric(string str)
        {
            char[] chr = str.ToCharArray();
            foreach (char ch in chr)
            {
                if ('0' > ch || ch > '9')
                    return false;

            }
            return true;
        }
        public static int getAzimuth(PointF pt)
        {
            double r, x, y;
            r = _size * 0.5d;//ban kinh duong tron
            //xac dinh toa do so voi tam
            x = pt.X - r;
            y = pt.Y - r;
            double canh_huyen = Math.Sqrt(x * x + y * y);
            //tinh goc so voi truc x
            double angle = Math.Acos(x / canh_huyen);
            //doi sang do.
            angle = angle / 0.0174532925d;

            if (y < 0)
            {
                angle = 360d - angle;
            }
            //chuyen sang goc so voi truc. y - Phuong BAC'

            double azimuth = angle - 270d;
            if (azimuth < 0) azimuth = azimuth + 360d;
            return (int)azimuth;
        }
        //lay goc phuong vi cua mot toa do da cho
        public static int getAzimuth(Coordinate td)
        {
            double r, x, y;
            r = _size * 0.5d;//ban kinh duong tron
            //xac dinh toa do so voi tam
            x = td.X - r;
            y = td.Y - r;
            double canh_huyen = Math.Sqrt(x * x + y * y);
            //tinh goc so voi truc x
            double angle = Math.Acos(x / canh_huyen);
            //doi sang do.
            angle = angle / 0.0174532925d;

            if (y < 0)
            {
                angle = 360d - angle;
            }
            //chuyen sang goc so voi truc. y - Phuong BAC'

            double azimuth = angle - 270d;
            if (azimuth < 0) azimuth = azimuth + 360d;
            return (int)azimuth;
        }
        // lay cu ly cua mot toa do da cho
        public static double getRange(Coordinate td)
        {
            double r, x, y;
            r = _size * 0.5d;//ban kinh duong tron
            //xac dinh toa do so voi tam
            x = td.X - r;
            y = td.Y - r;
            double canh_huyen = Math.Sqrt(x * x + y * y);
            return canh_huyen;
        }
        private void OnImageUpdate(Image i)
        {
            if (ImageUpdate != null)
                ImageUpdate(this, new ImageUpdateEventArgs(i));
        }

        /// <summary>
        /// Event fired when the output image is redrawn
        /// </summary>
        public event ImageUpdateHandler ImageUpdate;

        public static int soVong { get; set; }

        internal void Dispose()
        {
            g.Dispose();
            
        }

        internal bool checkMap(int toadoX, int toadoY)
        {
            bool isMap = false;
            if (currentObject!=null)
            {
                if ((Convert.ToInt16(currentObject.X) - 10) <= toadoX && (Convert.ToInt16(currentObject.X) + 10) >= toadoX && (Convert.ToInt16(currentObject.Y) - 10) <= toadoY && (Convert.ToInt16(currentObject.Y) + 10) >= toadoY)
                {
                    isMap = true;
                }
            }
                   
            return isMap;
        }

        public Coordinate currentObject { get; set; }

        public string pathIcon { get; set; }
    }
    public delegate void ImageUpdateHandler(object sender, ImageUpdateEventArgs e);
    public class ImageUpdateEventArgs : EventArgs
    {
        Image _image;

        public Image Image
        {
            get
            {
                return _image;
            }
        }

        public ImageUpdateEventArgs(Image i)
        {
            _image = i;
        }

    }
    
}

